package com.shujiaotong.controller;


import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shujiaotong.common.R;
import com.shujiaotong.pojo.Classes;
import com.shujiaotong.pojo.Tags;
import com.shujiaotong.service.IClassesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@RestController
@RequestMapping("/classes")
@Slf4j
public class ClassesController {

    @Autowired
    private IClassesService classesService;

    @GetMapping("page")
    public R page(
            @RequestParam int page,
            @RequestParam int pageSize,
            @RequestParam(required = false) String name){
//        接收参数
        log.info("当前页:{},页条数:{},姓名:{}",page,pageSize,name);
//        分页条件
        Page<Classes> classesPage = new Page<>(page,pageSize);
//        查询条件
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(!StringUtils.isEmpty(name),Classes::getName,name);
        qw.eq(Classes::getIsDeleted,0);
        qw.orderByDesc(Classes::getUpdateTime);
        qw.orderByAsc(Classes::getName);
//        分页查询
        classesService.page(classesPage,qw);
//        返回值
        return R.success(classesPage);
    }


//根据id查询
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id ){
//        log.info("根据id查询班级:{}",id);
        Classes classes = classesService.getById(id);
        return R.success(classes);
    }

//    修改
    @PutMapping
    public R update(@RequestBody Classes classes){
//        log.info("修改班级:{}",classes);
        //根据名字查询user表，更新class里的id字段
        boolean b = classesService.updateAllById(classes);
        return b?R.success("修改成功"):R.error("修改失败");

    }


//    新增
    @PostMapping
    public R save(@RequestBody Classes classes){
//        log.info("新增班级:{}",classes);
        boolean b = classesService.saveAll(classes);
        return b?R.success("新增成功"):R.error("新增失败");
    }

//    分页1
    @GetMapping("page1")
    public R page1(
            @RequestParam int page,
            @RequestParam int pageSize,
            @RequestParam(required = false) String name){
//        分页条件
        Page<Classes> classesPage = new Page<>(page,pageSize);
//        查询条件
        LambdaQueryWrapper<Classes> lqw = new LambdaQueryWrapper<>();
        lqw.like(!StringUtils.isEmpty(name),Classes::getName,name);
        lqw.eq(Classes::getIsDeleted,1);
        lqw.orderByDesc(Classes::getUpdateTime);
        lqw.orderByAsc(Classes::getName);
//        返回
        return R.success(classesService.page(classesPage,lqw));
    }

//    停用课程
    @DeleteMapping("delete")
    public R changeStatusTo1(Long id){
        Classes classes = classesService.getById(id);
        classes.setIsDeleted(1);
        boolean b = classesService.updateById(classes);
        return b? R.success("删除成功"):R.error("删除失败");
    }

//    恢复课程
    @DeleteMapping("restore")
    public R changeStatusTo0(Long id){
        Classes classes = classesService.getById(id);
        classes.setIsDeleted(0);
        boolean b = classesService.updateById(classes);
        return b? R.success("恢复成功"):R.error("恢复失败");
    }

//    查询选了该课程的所有用户

    @GetMapping("user/{id}")
    public R getUserByClassId(@PathVariable Long id){
        return classesService.getUserByClassId(id);
    }

//    修改状态
    @PutMapping("changeStatus")
    public R changeStatus(@RequestBody Classes classes){
        Classes classes1 = classesService.getById(classes.getId());
        if (classes1.getStatus()==0){
            classes1.setStatus(1);
            return classesService.updateById(classes1)?R.success("启用成功"):R.error("启用失败");
        }else {
            classes1.setStatus(0);
            return classesService.updateById(classes1)?R.success("禁用成功"):R.error("禁用失败");
        }
    }

//    获取会员卡的标签
    @GetMapping("getClassByCardTags")
    public R getClassByCardTags(@RequestParam String tags){
        List<String> list = classesService.getClassByCardTags(tags);
        return R.success(list);
    }

//    根据标签查询课程
// 根据标签名称查询课程信息
    @GetMapping("/getClassList/{tag}")
    public R getClassListByTag(@PathVariable String tag){
        List<Classes> list = classesService.getClassListByTag(tag);
        return R.success(list);
    }
}
